package com.xiaotu.spider.license.pageprocessor;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVLicenseModel;
import com.xiaotu.common.util.CacheHandler;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 TVLicensePageProcessor
 * @日期 2017年12月04日
 * @作者 WangYanLong
 * @功能 电视剧发行许可通告
 */
@Component("TVLicensePageProcessor")
public class TVLicensePageProcessor extends AUrlPageProcessor
{
	private String start_url = "https://www.baidu.com/";
    @Autowired
    private CacheHandler cacheHandler;
    private static final String filePath = PropertiesUtil.getProperty("filePath");
    
    @SuppressWarnings("unchecked")
    @Override
    public void process(Page page)
    {
        this.pageProcess(page);
    }

    private void pageProcess(Page page)
    {
    	try {
			Connection conn = HttpUtils.connent("https://dsj.nrta.gov.cn/tims/site/views/applications.shanty?appName=announce");
			conn.get();
			Response rs = conn.response();
			String html = rs.body();
			if(StringUtil.isNotEmpty(html)){
				Document documentHtml = Jsoup.parse(html,"utf8");
				Elements linkList = documentHtml.select("table[align=center]").select("tbody tr td").select("a[target=_blank]");
				Set<String> linkSet = new HashSet<String>();
				for(Element linkElementList : linkList){
					String linkUrl = linkElementList.select("a").attr("href");
					if(StringUtils.isNotBlank(linkUrl)){
						linkUrl = linkUrl.replace("/tims/site/", "https://dsj.nrta.gov.cn/tims/site/");
					}
					if(!cacheHandler.hExists(Constants.DataRedisKey.License_History.toString(),linkUrl)){
						linkSet.add(linkUrl);
					}
				}
				
					if(!linkSet.isEmpty()){
						this.tvProcess(page,linkSet);
					}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    private void tvProcess(Page page,Set<String> linkSet){
	try {
		Set<TVLicenseModel> tvSet = new HashSet<>();
		for (String linkUrl : linkSet) {
    	Connection conn = HttpUtils.connent(linkUrl);
		conn.get();
		Response rs = conn.response();
		String html = rs.body();
		if(StringUtil.isNotEmpty(html)){
		Document documentHtml = Jsoup.parse(html,"utf8");
		Html divHtml = new Html(documentHtml.toString());
    	String fileUrl = divHtml.xpath("//td[@class='newstext']/p/a/@href").toString();
		if(StringUtil.isEmpty(fileUrl)){
			fileUrl = divHtml.xpath("//td[@class='newstext']/p/*/a/@href").toString();
			if(StringUtil.isEmpty(fileUrl)){
				fileUrl = divHtml.xpath("//td[@class='newstext']/p/span/span/a/@href").toString();
				if(StringUtil.isEmpty(fileUrl)){
					fileUrl = divHtml.xpath("//td[@class='newstext']/p/span/span[@lang='EN-US']/span/a/@href").toString();
					if(StringUtil.isEmpty(fileUrl)){
						return;
					}	
				}
			}
		}
		fileUrl = fileUrl.replace("/resource/files/", "https://dsj.nrta.gov.cn/resource/files/");
		if(!fileUrl.contains("/shanty")){
			fileUrl = fileUrl.replace("https://dsj.nrta.gov.cn/", "https://dsj.nrta.gov.cn/shanty/");
		}
		//下载Excel文件
		String filename=fileUrl.substring(fileUrl.lastIndexOf("/"));
		FileUtil.saveToFile(fileUrl, filePath+filename);
		
		page.putField("filename", filename);
    	File file = new File(filePath+filename);
    	//读取Excel文件内容
		List<Map<String, String>> dataList = FileUtil.getExcelContents(file);
		if(dataList.size()==0){
			throw new SpiderException(SpiderExceptionCode.DataInvalideException);
		}
		for (Map<String, String> dataMap : dataList) {
			String tvname = dataMap.get("3");
			if (StringUtil.isEmpty(tvname) || tvname.equals("")|| tvname.equals("剧名")) {
				continue;
			}
			tvname = tvname.replaceAll("\\n", "");
			tvname = tvname.replace("《", "");
			tvname = tvname.replace("》", "");

			String sets = dataMap.get("4");
			sets = sets.replace(".0", "");
			
			String producecompany = dataMap.get("2");
			producecompany = producecompany.replaceAll("\\n", "");
			
			String licensenum = dataMap.get("5");
			licensenum = licensenum.replaceAll("\\n", "");
			
			String licensedate = dataMap.get("6");
			licensedate = licensedate.replace("/", "-").trim();
			if(licensedate.matches("^\\d{2}-\\d{1,2}-\\d{1,2}$")){
				licensedate="20"+licensedate;
			}
			
			if(licensedate.matches("^\\d{4}-\\d{1}-\\d{1}")){
				licensedate=licensedate.replace("-", "-0");
			}
			
			if(licensedate.matches("^\\d{4}-\\d{2}-\\d{1}")){
				licensedate=licensedate.substring(0, licensedate.lastIndexOf("-"))
						+"-0"
						+licensedate.substring(licensedate.lastIndexOf("-")+1, licensedate.length());
			}
			
			if(licensedate.matches("^\\d{4}-\\d{1}-\\d{2}")){
				licensedate=licensedate.substring(0, licensedate.indexOf("-"))
						+"-0"
						+licensedate.substring(licensedate.indexOf("-")+1, licensedate.length());
			}
			
			 String reviewcompany = dataMap.get("1");
			 reviewcompany = reviewcompany.replaceAll("\\n", "");
			 tvSet.add(new TVLicenseModel(Constants.TargetType.OTHER_TYPE_ID, tvname, sets,
					 producecompany,licensenum,licensedate, reviewcompany));
			}
		}
	 }
			if (!tvSet.isEmpty()){
				page.putField(TVLicenseModel.class.getName(), tvSet);
				page.putField(URL_KEY,linkSet);
			}
		
	} catch (IOException e) {
		e.printStackTrace();
	}
}

    @Override
    protected String[] getStartRequestUrls()
    {
        //return new String[]{"https://dsj.nrta.gov.cn/tims/site/views/applications.shanty?appName=announce"};
    	return new String[]{start_url};
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.TV_LICENSE;
    }

    @Override
    public String getPageDataType(Page page)
    {
        return Constants.PageDataType.SEARCH_LIST;
    }

}
